package userservice

import (
	"context"
	"gitee.com/go-mid/infra/xlog"
	"gitee.com/go-mid/infra/xtime"
	"gitee.com/go-mid/user/internal/component"
	"gitee.com/go-mid/user/internal/entity"
	"github.com/jinzhu/copier"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type GetUserLoginidReq struct {
	LoginID //  用户登录凭证表唯一标识
} //@name 获取用户登录凭证表入参

type UserLoginidInfo struct {
	ID          int64  `json:"id" bdb:"id"`                     //
	Ct          int64  `json:"ct" bdb:"ct"`                     //创建时间
	Ut          int64  `json:"ut" bdb:"ut"`                     //修改时间
	State       int32  `json:"state" bdb:"state"`               //状态。1正常，2注销
	Appid       int64  `json:"appid" bdb:"appid"`               //appid
	UserID      int64  `json:"user_id" bdb:"user_id"`           //用户id
	LoginidType int32  `json:"loginid_type" bdb:"loginid_type"` //注册类型。1手机号；2微信
	Loginid     string `json:"loginid" bdb:"loginid"`           //登录凭证
	UserPoolID  int64  `json:"user_pool_id" bdb:"user_pool_id"` //用户池子id

} //@name 用户登录凭证表信息

type GetUserLoginidRes struct {
	UserLoginidInfo *UserLoginidInfo `json:"user_loginid_info"`
} //@name 获取用户登录凭证表响应

func (m *UserServiceImpl) GetUserLoginid(ctx context.Context, req *GetUserLoginidReq) (res *GetUserLoginidRes, err error) {
	fun := "UserServiceImpl.GetUserLoginid -->"
	st := xtime.NewTimeStat()
	defer func() {
		xlog.Infow(ctx, fun, "durationMs", st.Millisecond(), "req", req, "res", res, "err", err)
	}()
	//这里 不能是空指针
	item, err := m.getLoginid(ctx, req.LoginID)
	if err != nil {
		xlog.Warnf(ctx, "%s getLoginid err: req: %v, err: %v", fun, req, err)
		err = status.Error(codes.Internal, err.Error())
		return
	}
	if item == nil {
		return nil, nil
	}
	res = &GetUserLoginidRes{
		UserLoginidInfo: m.toUserLoginidInfo(ctx, item),
	}
	return
}

func (m *UserServiceImpl) toUserLoginidInfo(ctx context.Context, item *entity.UserLoginid) *UserLoginidInfo {
	if item == nil {
		return nil
	}
	var info UserLoginidInfo
	copier.Copy(&info, item)
	return &info
}
func (m *UserServiceImpl) toUserLoginidInfos(ctx context.Context, items []*entity.UserLoginid) []*UserLoginidInfo {
	var infos = make([]*UserLoginidInfo, 0)
	for _, item := range items {
		infos = append(infos, m.toUserLoginidInfo(ctx, item))
	}
	return infos
}

type ListUserLoginidReq struct {
	UserID int64 `json:"user_id"` // 用户id
	//用户池id
	UserPoolID int64 `json:"user_pool_id"`
} //@name 查询用户登录凭证表入参

type ListUserLoginidRes struct {
	Items []*UserLoginidInfo `json:"items"`
} //@name 查询用户登录凭证表响应

func (m *UserServiceImpl) ListUserLoginid(ctx context.Context, req *ListUserLoginidReq) (res *ListUserLoginidRes, err error) {
	fun := "UserServiceImpl.ListUserLoginid -->"
	st := xtime.NewTimeStat()
	defer func() {
		xlog.Infow(ctx, fun, "durationMs", st.Millisecond(), "req", req, "res", res, "err", err)
	}()
	var items []*entity.UserLoginid
	var offset int64 = 0
	var limit int64 = 1000
	var where = map[string]interface{}{
		"user_pool_id": req.UserPoolID,
		"user_id":      req.UserID,
		"_limit":       []int64{offset, limit + 1},
		"_orderby":     "ct desc",
	}
	err = component.XDBUser.Select(ctx, entity.UserLoginidTable, where, &items)
	if err != nil {
		xlog.Warnf(ctx, "%s select err: req: %v, err: %v", fun, req, err)
		err = status.Error(codes.Internal, err.Error())
		return
	}
	if len(items) > int(limit) {
		items = items[0:limit]
	}
	offset = offset + int64(len(items))
	res = &ListUserLoginidRes{
		Items: m.toUserLoginidInfos(ctx, items),
	}
	return
}
